package com.clannad.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clannad.commonutils.R;
import com.clannad.eduservice.entity.EduTeacher;
import com.clannad.eduservice.entity.vo.TeacherQuery;
import com.clannad.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author clannad
 * @since 2022-07-31
 */
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
@Api(tags = "教师管理模块")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/findAll")
    @ApiOperation("查找所有的教师信息")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }
    //逻辑删除
    @DeleteMapping("/{id}")
    @ApiOperation("删除教师信息")
    public R removeTeacher( @ApiParam("教师ID") @PathVariable String id){
        boolean flag = eduTeacherService.removeTeacher(id);
        if (flag) {
            return R.ok();
        }
        else{
            return R.error();
        }
    }

    @GetMapping("/pageTeacher/{current}/{limit}")
    @ApiOperation("分页查询教师信息")
    public R pageTeacher(@PathVariable long current,@PathVariable long limit){
        Page<EduTeacher> page=new Page<>(current,limit);
        eduTeacherService.page(page, null);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @PostMapping("pageTeacherCondition/{current}/{limit}")
    @ApiOperation("多条件查询教师信息")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,@RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page=new Page<>(current,limit);
        LambdaQueryWrapper<EduTeacher> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            lambdaQueryWrapper.like(EduTeacher::getName,name);
        }
        if(!StringUtils.isEmpty(level)){
            lambdaQueryWrapper.eq(EduTeacher::getLevel,level);
        }
        if(!StringUtils.isEmpty(begin)){
            lambdaQueryWrapper.ge(EduTeacher::getGmtCreate,begin);
        }
        if(!StringUtils.isEmpty(end)){
            lambdaQueryWrapper.le(EduTeacher::getGmtCreate,end);
        }
        lambdaQueryWrapper.orderByDesc(EduTeacher::getGmtCreate);
        eduTeacherService.page(page, lambdaQueryWrapper);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @PostMapping("addTeacher")
    @ApiOperation("添加教师信息")
    public R addTeacher(@RequestBody EduTeacher teacher){
        boolean save = eduTeacherService.saveTeacher(teacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("getTeacher/{id}")
    @ApiOperation("根据id查询教师信息")
    public R getTeacher(@PathVariable String id){
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher",byId);
    }

    @PostMapping("/updateTeacher")
    @ApiOperation("更新教师信息")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        boolean b = eduTeacherService.updateTeacher(teacher);
        if (b) {
            return R.ok();
        }else{
            return R.error();
        }
    }



}


